package com.qiyu.canal.client;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author idea
 * @Date created in 10:11 上午 2020/8/2
 */
public class CanalHandlerImpl implements CanalHandler {

    @Override
    public void dataHandle(List<CanalEntry.Entry> entryList) throws InvalidProtocolBufferException {
        for (CanalEntry.Entry entry : entryList) {
            if (entry.getEntryType() == CanalEntry.EntryType.ROWDATA) {
                CanalEntry.RowChange rowChange= (CanalEntry.RowChange) dataEntryHandle(entry);
                System.out.println(rowChange);
            }
        }
    }

    @Override
    public Object dataEntryHandle(CanalEntry.Entry entry) throws InvalidProtocolBufferException {
        CanalEntry.RowChange rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        CanalEntry.EventType eventType = rowChange.getEventType();
        if (eventType == CanalEntry.EventType.DELETE) {
           return CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        } else if (eventType == CanalEntry.EventType.UPDATE) {
            return CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        } else if (eventType == CanalEntry.EventType.INSERT) {
            return CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        }
        return null;
    }

}
